﻿using IdentityServer4;
using IdentityServer4.EntityFramework.DbContexts;
using IdentityServer4.EntityFramework.Mappers;
using IdentityServer4.Models;
using Microsoft.EntityFrameworkCore;

namespace IdentityServer.Extensions
{
    public class ConfigData
    {
        public static void InitData(IApplicationBuilder applicationBuilder)
        {
            Console.WriteLine("Starting Initing Data...");
            using (var scope = applicationBuilder.ApplicationServices.CreateScope())
            {
                scope.ServiceProvider.GetRequiredService<PersistedGrantDbContext>().Database.Migrate();
                {
                    var context = scope.ServiceProvider.GetRequiredService<ConfigurationDbContext>();
                    context.Database.Migrate();
                    CreateData(context);
                }
            }
            Console.WriteLine("Creating Data is Finished!");
        }

        public static void CreateData(ConfigurationDbContext context)
        {
            if (!context.Clients.Any())
            {
                Console.WriteLine("Clients is Initing...");
                foreach (var client in GetClients())
                {
                    context.Clients.Add(client.ToEntity());
                }
                context.SaveChanges();
            }
            if (!context.IdentityResources.Any())
            {
                Console.WriteLine("IdentityResources is Initing...");
                foreach (var resource in GetIdentityResources())
                {
                    context.IdentityResources.Add(resource.ToEntity());
                }
                context.SaveChanges();
            }
            if (!context.ApiResources.Any())
            {
                Console.WriteLine("ApiResources is Initing...");
                foreach (var resource in GetResources())
                {
                    context.ApiResources.Add(resource.ToEntity());
                }
                context.SaveChanges();
            }
        }

        public static IEnumerable<ApiResource> GetResources()
        {
            return new List<ApiResource>
            {
                new ApiResource("apiclient", "apiclient"),
                new ApiResource("api", "api")
            };
        }

        public static IEnumerable<IdentityResource> GetIdentityResources()
        {
            return new List<IdentityResource>
            {
                new IdentityResources.OpenId(),
                new IdentityResources.Profile()
            };
        }

        public static IEnumerable<Client> GetClients()
        {
            return new List<Client>
            {
                new Client
                {
                    ClientId = "Inner.client.code",
                    AllowedGrantTypes = GrantTypes.ClientCredentials,
                    AccessTokenLifetime = 3600*24*365, // 365天
                    ClientSecrets =
                    {
                        new Secret("Inner.secret.code".Sha256())
                    },
                    AllowedScopes = { "apiclient" }
                },
                new Client
                {
                    ClientId = "pwd",
                    AllowedGrantTypes = GrantTypes.ResourceOwnerPassword,
                    AccessTokenLifetime = 3600, //1h
                    SlidingRefreshTokenLifetime =  3600*24*3, //刷新token 3天
                    RefreshTokenExpiration = TokenExpiration.Sliding,
                    ClientSecrets =
                    {
                        new Secret("secret".Sha256())
                    },
                    AllowedScopes = { "api",IdentityServerConstants.StandardScopes.OfflineAccess},
                    AllowOfflineAccess = true
                }
            };
        }
    }
}
